/** Copyright (c) 2012, peter.ducai@gmail.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the Peter Ducai nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL PETER "daemonna" DUCAI BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.daemonna.fluentlee_core.utils;

import java.io.*;

/*
 * @author peter 'daemonna' ducai @author peter.ducai@gmail.com @author
 * http://code.google.com/p/fluentlee/
 */
public class file_util {

    public file_util() {
    }

    public File rename_file(File old_file, String new_name) {
        /*
         * Many aspects of the behavior of this method are inherently
         * platform-dependent: The rename operation might not be able to move a
         * file from one filesystem to another, it might not be atomic, and it
         * might not succeed if a file with the destination abstract pathname
         * already exists. The return value should always be checked to make
         * sure that the rename operation was successful.
         */

        // File (or directory) with new name
        File new_file = new File(new_name);

        // Rename file (or directory)
        boolean success = old_file.renameTo(new_file);
        if (!success) {
            // File was not successfully renamed
        }

        return new_file;
    }

    public void move_file(String old_name, String new_name) {

        System.out.println("moving "+old_name+" to "+new_name);
        
        // File (or directory) with new name
        File new_file = new File(new_name);
        File old_file = new File(old_name);

        // Rename file (or directory)
        boolean success = old_file.renameTo(new_file);
        if (success) {
            old_file.delete();
        }
    }

    public File create_temp_file(String temp_name) {
        File temp = null;
        try {
            // Create temp file.
            temp = File.createTempFile(temp_name, ".temp");

            // Delete temp file when program exits.
            temp.deleteOnExit();
            try (BufferedWriter out = new BufferedWriter(new FileWriter(temp))) {
                out.write("aString");
            }
        } catch (IOException e) {
        }
        return temp;
    }

    public void copy_file(File src, File dst) throws IOException {
        OutputStream out;
        try (InputStream in = new FileInputStream(src)) {
            out = new FileOutputStream(dst);
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
        out.close();
    }

    public void rewrite_file_timestamp(String tfile) {
        File file = new File(tfile);

        // Get the last modified time
        long modifiedTime = file.lastModified();
        // 0L is returned if the file does not exist

        // Set the last modified time
        long newModifiedTime = System.currentTimeMillis();
        boolean success = file.setLastModified(newModifiedTime);
        if (!success) {
            // operation failed.
        }
    }
    
    public void rewrite_file_timestamp(String tfile, long new_timestamp) {
        File file = new File(tfile);

        // Get the last modified time
        long modifiedTime = file.lastModified();
        // 0L is returned if the file does not exist

        // Set the last modified time
        long newModifiedTime = new_timestamp;
        boolean success = file.setLastModified(newModifiedTime);
        if (!success) {
            // operation failed.
        }
    }

    public void create_file_checksum() {
    }
}
